//------------------------------------------------------------------------------
// <copyright file="WebDataService.svc.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using NCT.INVPOS.DataAccessLayer;
using NCT.INVPOS.DataAccessLayer.Entity;
using NCT.INVPOS.DataAccessLayer.Repositories;

namespace NCT.INVPOS.WEBSERVICE
{
    [System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]   
    public class NCTWCFService : DataService<AppContext>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            // Examples:
            // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);
            // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
            config.UseVerboseErrors = true;  
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }

        protected override AppContext CreateDataSource()
        {
            var appcontext = new AppContext();

            //var oContext = ((IObjectContextAdapter)appcontext).ObjectContext;
            //appcontext.Configuration.ProxyCreationEnabled = false;
            // Return the underlying context. 
            return appcontext;
        }

        [WebGet]
        public void CheckSaving()
        {
            Product pro = new Product()
            {
                Name = "asdaaaaa",
                Code = "asd",
                Description = "asd",
                Branch = new Branch()
                {
                    Name = "asd",
                    IsActive = true
                }
            };

            this.CurrentDataSource.Products.Add(pro);
           this.CurrentDataSource.SaveChanges();
        }
    }
}
